<template>
    <div class="taskReport">
        <wtop></wtop>
        <div class="center">
            <div class="right">
                <!-- <div class="where">当前位置：外呼系统><router-link to="/callsystem/taskReport">数据报表</router-link></div> -->
                <!-- <div class="topTag">
                  <div class="topLogo"><img :src="srclogo">管理中心</div>
                  <div class="powertop">
                    <router-link class="powertopli powertopact" to="/callsystem/taskReport">数据报表</router-link>
                    <router-link class="powertopli" to="/callsystem/personReport">人员报表</router-link>
                    <router-link class="powertopli" to="/callsystem/taskControl">任务管理</router-link>
                  </div>
                </div> -->
                <div style="height: 16px; width: 100%; background: #EFEFEF;"></div>

                <div class="conlist" v-loading='loading'>
                    <div class="flex">
                        <div class="gong">
                          <div class="flex">
                            <div class="data-box">
                              <img class="order-icon" src="../../../static/img/waihu_1.png">
                              <div class="tit">累计发布任务数</div>
                              <div class="num">{{topList.task_count}}</div>
                            </div>
                            <div class="data-box">
                              <img class="order-icon" src="../../../static/img/waihu_2.png">
                              <div class="tit">累计拨打用户数</div>
                              <div class="num">{{topList.called_all}}</div>
                            </div>
                            <div class="data-box">
                              <img class="order-icon" src="../../../static/img/waihu_3.png">
                              <div class="tit">拨通用户数</div>
                              <div class="num">{{topList.called_count}}</div>
                            </div>
                            <div class="data-box">
                              <img class="order-icon" src="../../../static/img/waihu_4.png">
                              <div class="tit">平均通话时长</div>
                              <div class="num">{{topList.avg_time}}s</div>
                            </div>
                            <div class="data-box">
                              <img class="order-icon" src="../../../static/img/waihu_5.png">
                              <div class="tit">业务成功率</div>
                              <div class="num">{{topList.success_rate}}%</div>
                            </div>
                          </div>
                          <div style="height:100%; width: 1px; background: #707070;"></div>
                          <div class="right-box">
                            <div class="data-box" style="height: 120px;">
                              <img class="order-icon" src="../../../static/img/waihu_6.png"  @click="downloadReport=true">
                              <div class="tit" @click="downloadReport=true">下载业绩结算表</div>
                              <div @click="goid('/order_export_waihuyeji2')" style="color: rgb(25, 137, 250);">
                                查看导出
                              </div>
                            </div>
                          </div>
                        </div>
                    </div>
                </div>
                <div style="height: 16px; width: 100%; background: #EFEFEF;"></div>
                <div class="powerTop">
                  <div class="powertopli" :class="tab == 1 ? 'powertopact' : ''" @click="tabChange(1)">任务报表</div>
                  <div class="powertopli" :class="tab == 2 ? 'powertopact' : ''" @click="tabChange(2)">人员报表</div>
                </div>
                <div class="conlist" v-loading='loading'>
                  <div v-if="tab == 1">
                    <div class="list">
                        <div class="th">任务名称</div>
                        <div class="th">任务类型</div>
                        <div class="th">用户数量</div>
                        <div class="th">拨打数量</div>
                        <div class="th">拨通数量</div>
                        <div class="th">拨通率
                          <el-tooltip effect="dark" content="拨打后接通的概率，算法：拨通数量/拨打数量" placement="top">
                            <i class="el-icon-question"></i>
                          </el-tooltip>
                        </div>
                        <div class="th">目标达成数量</div>
                        <div class="th">业务成功率
                          <el-tooltip effect="dark" content="发生沟通后业务成功的概率，算法：目标达成数量/拨通数量" placement="top">
                            <i class="el-icon-question"></i>
                          </el-tooltip>
                        </div>
                    </div>
                    <div class="list" v-for="(item,index) in list" :key="index">
                        <div class="td">{{item.name}}</div>
                        <div class="td">{{item.type_name}}</div>
                        <div class="td">{{item.count}}</div>
                        <div class="td">{{item.done}}</div>
                        <div class="td">{{item.called_ok}}</div>
                        <div class="td">{{item.ok_rate}}%</div>
                        <div class="td">{{item.success_amount}}</div>
                        <div class="td">{{item.success_rate}}%</div>
                    </div>
                    <el-pagination background layout="total,prev, pager, next" :current-page="page" @current-change="fanye" :page-size="pagesize" :total="total"></el-pagination>
                  </div>
                  <div v-if="tab == 2" v-loading="kidloding">
                    <div class="list">
                        <div class="th">部门</div>
                        <div class="th">岗位</div>
                        <div class="th">人员</div>
                        <div class="th">拨打数量</div>
                        <div class="th">拨通数量</div>
                        <div class="th">短信联系数量</div>
                        <div class="th">拨通率
                          <el-tooltip effect="dark" content="拨打后接通的概率，算法：拨通数量/拨打数量" placement="top">
                            <i class="el-icon-question"></i>
                          </el-tooltip>
                        </div>
                        <div class="th">平均拨打成本
                          <el-tooltip effect="dark" content="已经发生的拨打成本，算法：拨打次数/拨打数量" placement="top">
                            <i class="el-icon-question"></i>
                          </el-tooltip>
                        </div>
                        <div class="th">目标达成数量</div>
                        <div class="th">业务成功率
                          <el-tooltip effect="dark" content="发生沟通后业务成功的概率，算法：目标达成数量/拨通数量" placement="top">
                            <i class="el-icon-question"></i>
                          </el-tooltip>
                        </div>
                        <div class="th td2">操作</div>
                    </div>
                    <el-collapse accordion>
                      <el-collapse-item v-for="(item,index) in personlist" :key="index">
                        <template slot="title">
                          <div class="list llst" @click="getJob(item)">
                            <div class="td td1">{{item.group_name}}</div>
                            <div class="td td1">{{item.job_name}}</div>
                            <div class="td td1">{{item.user_amount}}</div>
                            <div class="td td1">{{item.called_amount}}</div>
                            <div class="td td1">{{item.called_ok}}</div>
                            <div class="td td1">{{item.sms_num}}</div>
                            <div class="td td1">{{item.callpass}}%</div>
                            <div class="td td1">{{item.callavg}}</div>
                            <div class="td td1">{{item.success_amount}}</div>
                            <div class="td td1">{{item.callsuccess}}%</div>
                            <div class="td td2 td1">
                              <el-button type="text" v-if="!item.slide">展开详情<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                              <el-button type="text" v-if="item.slide">收起详情<i class="el-icon-arrow-up el-icon--right"></i></el-button>
                            </div>
                          </div>
                        </template>
                        <div class="list llst" v-for="items in personList" :key="items.id">
                          <div class="td"></div>
                          <div class="td"></div>
                          <div class="td">{{items.user_name}}</div>
                          <div class="td">{{items.called_amount}}</div>
                          <div class="td">{{items.called_ok}}</div>
                          <div class="td">{{item.sms_num}}</div>
                          <div class="td">{{items.callpass}}%</div>
                          <div class="td">{{items.callavg}}</div>
                          <div class="td">{{items.success_amount}}</div>
                          <div class="td">{{items.callsuccess}}%</div>
                          <div class="td td2">
                            <el-button type="text" @click="detail(items.id)">查看详情</el-button>
                          </div>
                        </div>
                      </el-collapse-item>
                    </el-collapse>

                    <el-pagination background layout="total,prev, pager, next" :current-page="page" @current-change="fanye" :page-size="pagesize" :total="total"></el-pagination>
                  </div>
                </div>
            </div>
        </div>
        <el-dialog title="选择日期" :visible.sync="downloadReport" :close-on-click-modal="false" width="600px">
            <div>
              <div style="margin-top: 20px;">选择日期：<el-date-picker v-model="time" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" placeholder="选择日期时间"></el-date-picker></div>
            </div>
            <span slot="footer" class="dialog-footer">
              <el-button @click="downloadReport = false">取 消</el-button>
              <el-button type="primary" @click="downloadTask">确 定</el-button>
            </span>
        </el-dialog>
        <wfoot></wfoot>
    </div>

</template>

<script>
    import axios from 'axios'
    import wtop from '@/components/callsystem/wtop.vue'
    import wfoot from '@/components/callsystem/wfoot.vue'
    import moment from "moment";
    import UploadExcelComponent from "@/components/UploadExcel/index.vue";

    export default {
        components: {
            "wtop": wtop,
            "wfoot": wfoot,
            UploadExcelComponent
        },
        name: "taskReport",
        data() {
            return {
                srclogo:"./static/img/wh_icon2.png",
                total:0,
                pagesize:20,
                page:1,
                topList: {
                  task_count: 0,
                  called_all: 0,
                  called_count: 0,
                  avg_time: 0,
                  success_rate: 0
                },
                list:[],
                personlist: [],
                personList: [],
                loading:false,
                releaseTask: false,
                downloadReport: false,
                time:[],
                url: '',
                tab: 1,
                kidloding: false
            }
        },
        created() {
          var user = JSON.parse(localStorage.getItem('USER'));
          this.user_func = user.functions
          for( var i in this.user_func){
              var func_number = this.user_func[i].code
              if(func_number.indexOf("K02") >= 0){ this.leader = 1 }
          }
          this.url = window.location.origin
          this.getData()
          this.getlist()
        },
        mounted() {

        },
        methods: {
          //详情
          detail(id){
            this.$router.push({
              path: "/callsystem/personDetail",
              query: { id: id }
            })
          },
          getJob(item){
            var that = this
            for(let i in that.personlist){
              if(that.personlist[i].id != item.id){
                that.personlist[i].slide = false
              }
            }
            this.kidloding = true
            item.slide = !item.slide
            let data = new FormData()
            data.id = item.id
            this.leader ? data.is_leader = this.leader : ''
            axios.get('/api/plan-market/job/call/list', {params:data})
              .then(response => {
                if(response.data.msg.code == 0){
                  that.personList = response.data.data.data
                  for(let i in that.personList){
                    if(that.personList[i].called_ok == null){
                      that.personList[i].called_ok = 0
                    }
                    if(that.personList[i].logs_count == null){
                      that.personList[i].logs_count = 0
                    }
                    // if(that.personList[i].called_amount == null){
                    //   that.personList[i].called_amount = 0
                    // }
                    if(that.personList[i].success_amount == null){
                      that.personList[i].success_amount = 0
                    }
                    that.personList[i].callpass = that.personList[i].called_amount ? Math.round(that.personList[i].called_ok/that.personList[i].called_amount*100) : 0
                    that.personList[i].callavg = that.personList[i].called_ok ? Math.round(that.personList[i].logs_count/that.personList[i].called_ok) : 0
                    that.personList[i].callsuccess = that.personList[i].called_ok ? Math.round(that.personList[i].success_amount/that.personList[i].called_ok*100) : 0
                  }
                  this.kidloding = false
                }else{
                  that.$message.error(response.data.msg.info);
                }
            })
          },
          //下载报表
          downloadTask(){
            if(this.time == "" || this.time == undefined){
                this.$message('请选择下载日期！');
                return
            }
            let day = this.dateDay(this.time[0],this.time[1])
            if(day > 62){
              this.$message.error(`您选择了${day}天，目前最多只允许62天哟^_^`);
              return
            }
            var that = this
              axios.get('/api/plan-market/call/order/total_data/export?start='+this.time[0]+'&end='+this.time[1])
                .then(response => {
                  if(response.data.msg.code == 0){
                    // var a = document.createElement("a");
                    // a.setAttribute("href", that.url+'/'+response.data.data);
                    // a.click();
                    // that.downloadReport = false
                    this.goid('/order_export_waihuyeji2')
                  }else{
                    that.$message.error(response.data.msg.info);
                  }
              })
            // var a = document.createElement("a");
            // a.setAttribute("href", this.url+'/api/plan-market/call/order/total_data/export?start='+this.time[0]+'&end='+this.time[1]);
            // a.click();
            // this.downloadReport = false
          },
          // downloadTask(){
          //   if(this.time == "" || this.time == undefined){
          //       this.$message('请选择下载日期！');
          //       return
          //   }
          //   let day = this.dateDay(this.time[0],this.time[1])
          //   if(day > 62){
          //     this.$message.error(`您选择了${day}天，目前最多只允许62天哟^_^`);
          //     return
          //   }
          //   var that = this
          //   axios.get('/api/plan-market/caller/results/export?start='+this.time[0]+'&end='+this.time[1])
          //     .then(response => {
          //       if(response.data.msg.code == 0){
          //         var a = document.createElement("a");
          //         a.setAttribute("href", that.url+'/'+response.data.data);
          //         // console.log(that.url+'/'+response.data.data);
          //         a.click();
          //         that.downloadReport = false
          //       }else{
          //         that.$message.error(response.data.msg.info);
          //       }
          //   })
          // },
          //时间区间转天数
          dateDay(start,end){
           let day = new Date(end).getTime() - new Date(start).getTime()
           day = day/24/60/60/1000
           return day
          },
            //翻页
            fanye:function(page){
            	this.loading = true
            	this.page = page
              if(this.tab == 1){
                this.getlist()
              }
              if(this.tab == 2){
                this.getPersonData()
              }
            },
            //顶部数据
            getData(){
              var that = this
              axios.get('/api/plan-market/shop/call/data')
              	.then(response => {
              		if(response.data.msg.code == 0){
                    that.topList = response.data.data
                    that.topList.avg_time = Math.round(that.topList.avg_time)
                    that.topList.success_rate = Math.round(that.topList.success_rate*100)
              		}else{
              			that.$message.error(response.data.msg.info);
              		}
              	})
            },
            tabChange:function(o) {
              this.tab = o;
              this.page = 1
              //调接口
              if(this.tab == 1){
                this.getlist()
              }
              if(this.tab == 2){
                this.getPersonData()
              }
            },
            //获取列表
            getlist:function(){
                var that = this
                this.loading = true
                axios.get(`/api/plan-market/shop/call/list?page=${this.page}&page_size=${this.pagesize}`)
                	.then(response => {
                		if(response.data.msg.code == 0){
                            that.list = response.data.data.data
                            that.total = response.data.data.total
                            for(let i in that.list){
                              if(that.list[i].ok_rate == null){
                                that.list[i].ok_rate = 0
                              }
                              that.list[i].ok_rate = Math.round(that.list[i].ok_rate*100)
                              if(that.list[i].success_rate == null){
                                that.list[i].success_rate = 0
                              }
                              that.list[i].success_rate = Math.round(that.list[i].success_rate*100)
                            }
                            that.loading = false
                		}else{
                			that.$message.error(response.data.msg.info);
                		}
                	})
            },
            getPersonData:function(){
              var that = this
              this.loading = true
              axios.get(`/api/plan-market/group/call/list?page=${this.page}&page_size=${this.pagesize}`)
                .then(response => {
                  if(response.data.msg.code == 0){
                          that.personlist = response.data.data.data
                          that.total = response.data.data.total
                          for(let i in that.personlist){
                            that.personlist[i].slide = false
                            if(that.personlist[i].called_ok == null){
                              that.personlist[i].called_ok = 0
                            }
                            if(that.personlist[i].logs_count == null){
                              that.personlist[i].logs_count = 0
                            }
                            // if(that.personlist[i].called_amount == null){
                            //   that.personlist[i].called_amount = 0
                            // }
                            if(that.personlist[i].success_amount == null){
                              that.personlist[i].success_amount = 0
                            }
                            that.personlist[i].callpass = that.personlist[i].called_amount ? Math.round(that.personlist[i].called_ok/that.personlist[i].called_amount*100) : 0
                            that.personlist[i].callavg = that.personlist[i].called_ok ? Math.round(that.personlist[i].logs_count/that.personlist[i].called_ok) : 0
                            that.personlist[i].callsuccess = that.personlist[i].called_ok ? Math.round(that.personlist[i].success_amount/that.personlist[i].called_ok*100) : 0
                          }
                          that.loading = false
                  }else{
                    that.$message.error(response.data.msg.info);
                  }
                })
            },
        }
    }
</script>

<style lang="scss" scoped>
    .taskReport{ margin: 0; padding: 0; background: #EFEFEF;
        .listupload{ font-size: 14px;
            .li{flex: 1; line-height: 38px; margin-top: 20px;}
            .el-button--primary{ background: #ffffff; color: #409EFF; border-color: #409EFF; }

        }
        .center{ min-width: 1366px;width: 1366px; margin: 0 auto; display: flex; justify-content: space-between;
    		.right{ flex-basis: 1366px; background: #ECECEC;
                .title{border-bottom: 1px #EAEAEA solid;  line-height: 45px; padding-left: 30px; font-size: 16px; background: #F9F9F9; color: #727272}
                .flex{
                  display: flex;
                  .data-box{
                    height: 120px;padding: 20px 60px 0px 70px;display: flex;flex-direction: column;justify-content: space-around;align-items: center;
                      .tit{font-size: 14px;}
                      .order-icon{width:46px;height:46px;}
                      .num{font-size: 20px;color: #FF4040;}
                  }
                }
                .right-box{
                  .data-box{height: 80px;;padding-right: 0px;cursor: pointer;}
                }

                i.el-collapse-item__arrow.el-icon-arrow-right{display: none;}
                .el-collapse-item__content{padding-bottom: 0;}
            }
            .topTag{display: flex;background: #fff;position: relative;border-bottom: #e2e0e3 1px solid;
              .topLogo{ font-size: 20px; color: #707070;display: flex;align-items: center;padding: 14px 0;
                img{ width: 30px;height: 30px;margin: 0 10px;}
              }
              .powertop{ display: flex; width: 100%;position: absolute;justify-content: center;}
              .powertopli{ cursor: pointer;flex-basis: 120px; line-height: 50px;padding-top: 5px; border-bottom: 3px #F8F8F8 solid; color: #707070; font-size: 18px; text-align: center;}
              .powertopact{border-bottom: 3px #4D4D4D solid;font-weight: bold;}
            }
            .powerTop{ display: flex; width: 100%;background: #fff;border-bottom: #D9D9D9 1px solid;
              .powertopli{ flex-basis: 140px; line-height: 55px; border-bottom: 3px #FFFFFF solid; color: #727272; font-size: 16px; text-align: center;cursor: pointer;}
              .powertopact{ background: #FFFFFF; border-bottom: 3px #FF6767 solid;color: #FF6767;}
            }
            .con{  background: #FFFFFF; padding: 30px 40px; display: flex; justify-content: space-between;
                .conleft{
                    .flex{ display: flex; margin-bottom: 20px;}
                    .gong{ margin-right: 30px; color: #333;
                    }
                }
                .conright{
                    .el-input__inner{ line-height: 40px !important; height: 40px !important;}
                }
            }
            .conlist{ padding: 30px; background: #FFFFFF;
                .flex{ display: flex; justify-content: space-between}
                .gong{ display: flex;
                    .el-date-editor.el-input{ width: 200px;}
                    .el-input__inner{ width: 250px;}
                    .el-button--danger{ margin-left: 20px;}
                    .el-button--primary{ padding: 10px 30px; background: #EEF7FF; border: 1px #EEF7FF solid; margin-left: 20px;}
                    .el-date-editor--month{ margin-right: 20px;}
                }
                .gl{ margin-bottom: 20px;
                    .el-input__inner{ width: 200px !important; }
                    .el-button--danger{ margin-left: 0px !important; margin-right: 20px;}
                }
                .ml{
                  .el-button--danger{ margin-left: 10px!important;}
                }
                .list{ display: flex; border-bottom: 1px #eaeaea solid; width: 100%;
                    .th{ background: #EAEAEA; text-align: center; flex: 1; padding: 15px 0; font-size: 14px;
                      .el-icon-question{color: #D2D2D2;font-size: 14px;}
                    }
                    .td{ background: #ffffff; text-align: center; flex: 1; padding: 15px 0; font-size: 14px;}
                    .td1{ padding: 0; }
                    .td2{
                      flex: 1.5;
                      .el-button{padding: 0;}
                    }
                    // .el-collapse{width: 100%;}
                }
                .llst{border-bottom: 0;align-items: center;}
                .el-pagination{ text-align: right; margin: 20px 0}
            }
    	}
      .formflex{display: flex;justify-content: space-between;
        .el-input__inner{ width: 200px; }
        .el-input{width: 100%!important;}
        .lineflex{display: flex;
          .el-button--text{height: 40px;margin-left: 20px;}
        }

      }
      .el-button--danger{padding: 14px 30px !important;}
      .el-tree__empty-text{position: initial!important;}
      .formsubmit{justify-content: center;}
      .protag{
      	.el-tag{ margin-right: 20px; margin-bottom: 20px;}
      }
    }
</style>
